package com.jshongli.myapplication.common.utils;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;


import com.jshongli.myapplication.MainActivity;
import com.jshongli.myapplication.R;

import java.util.List;
import java.util.Map;
import java.util.Random;

import static android.content.Context.NOTIFICATION_SERVICE;


public class NotificationUtil {


    public static  NotificationCompat.Builder getChannelNotification(Context mContext, PendingIntent intent) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return null;
        }
        int i = 0;
        return new NotificationCompat.Builder(mContext, "1").setLargeIcon(BitmapFactory.decodeResource(mContext.getResources
                (), R.mipmap.ic_launcher)).setSmallIcon(R.mipmap.ic_launcher).setContentIntent(intent).setContentTitle("标题").setContentText("内容")
                .setAutoCancel(true).setShowWhen(true).setVisibility(Notification.VISIBILITY_PUBLIC).setPriority(NotificationCompat.PRIORITY_HIGH);
    }


//    public static void set1(Context mContext,String msg){
//        Intent intent = new Intent(mContext, HomeActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
//        NotificationCompat.Builder builder = getChannelNotification(mContext,pendingIntent);
//        NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
//        //高版本需要渠道，很多人弹不出消息通知，就是因为没加这个
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            //只在Android O之上需要渠道
//            NotificationChannel notificationChannel = new NotificationChannel("channelid1", "channelname", NotificationManager.IMPORTANCE_HIGH);
//            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道，通知才能正常弹出
//            manager.createNotificationChannel(notificationChannel);
//        }
//        Notification notification;
//        notification = builder.build();
//        int notifyId = (int) ((Math.random() * 10));
//        manager.notify(notifyId, notification);
//    }

    public static boolean isAppAlive(Context context, String packageName){
        ActivityManager activityManager =
                (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfos
                = activityManager.getRunningAppProcesses();
        for(int i = 0; i < processInfos.size(); i++){
            if(processInfos.get(i).processName.equals(packageName)){
                Log.i("NotificationLaunch",
                        String.format("the %s is running, isAppAlive return true", packageName));
                return true;
            }
        }
        Log.i("NotificationLaunch",
                String.format("the %s is not running, isAppAlive return false", packageName));
        return false;
    }

    public static void setNotification(Context mContext, String msg) {

//        int i = Integer.valueOf(msgId);
        //点击通知栏消息跳转页

//        int androidRole = UtilPreferences.getInt(mContext,getString(R.string.androidRole));
        Intent intent = null;
        if(isAppAlive(mContext,"com.jshongli.wspf_cz")){
//            if(PatrolActivity.mActivity!=null){
//                intent = new Intent(mContext, PatrolActivity.class);
//            }
        }else {
            intent = new Intent(mContext, MainActivity.class);
//            if(androidRole == 2){
//                intent = new Intent(mContext, MerchantHomeActivity.class);
//            }else if(androidRole == 3){
//                intent = new Intent(mContext, ReplenishHomeActivity.class);
//            }else {
//                intent = new Intent(mContext, HomeActivity.class);
//            }
        }

//        intent.putExtra("isReceive",true);

        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        //创建通知消息管理类
        NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
        //高版本需要渠道，很多人弹不出消息通知，就是因为没加这个
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel("channelid1", "channelname", NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道，通知才能正常弹出
            manager.createNotificationChannel(notificationChannel);
        }

//        RemoteViews rv = new RemoteViews(mContext.getPackageName(),R.layout.view_notification);
//        rv.setTextViewText(R.id.tv_content,msg);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, "channelid1")//创建通知消息实例
                .setContentTitle("智慧排水综合平台")
                .setContentText(msg)
//                .setContent()
                .setWhen(System.currentTimeMillis())//通知栏显示时间
                .setSmallIcon(R.mipmap.logo2)//通知栏小图标
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.logo))//通知栏下拉是图标
                .setContentIntent(pendingIntent)//关联点击通知栏跳转页面
                .setPriority(NotificationCompat.PRIORITY_HIGH)//设置通知消息优先级
                .setAutoCancel(true)//设置点击通知栏消息后，通知消息自动消失
//                .setSound(Uri.fromFile(new File("/system/MP3/music.mp3"))) //通知栏消息提示音
                .setVibrate(new long[]{0, 1000, 1000, 1000}) //通知栏消息震动
                .setLights(Color.GREEN, 1000, 2000) //通知栏消息闪灯(亮一秒间隔两秒再亮)
                .setDefaults(NotificationCompat.DEFAULT_ALL); //通知栏提示音、震动、闪灯等都设置为默认
        //短文本
        Notification notification;
        notification = builder.build();
        //Constant.TYPE1为通知栏消息标识符，每个id都是不同的
        manager.notify(1, notification);
    }

//    public static void test1(Context mContext, String msg) {
//        NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//
//        final Notification.Builder builder = new Notification.Builder(mContext);
//        Intent intent = new Intent(mContext, HomeActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
//        builder.setContentIntent(pendingIntent);//普通消息
//        builder.setWhen(System.currentTimeMillis());
//        builder.setSmallIcon(R.mipmap.ic_launcher);
//        builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
//        builder.setAutoCancel(true);
//        builder.setPriority(Notification.PRIORITY_HIGH);
//        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
//        }
//        builder.setContentTitle("自定义消息标题");
//        builder.setContentText("自定义消息内容");
//        int notifyId = (int) ((Math.random() * 10));
//        nm.notify(notifyId, builder.build());//普通消息提示 显示在状态栏
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//SDK版本大于等于21才有悬挂式通知栏
//            Intent xintent = new Intent(mContext, HomeActivity.class);
//            xintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            PendingIntent hangIntent = PendingIntent.getActivity(mContext, 0, xintent, PendingIntent.FLAG_CANCEL_CURRENT);
//            builder.setFullScreenIntent(hangIntent, true);//悬挂式通知 悬挂在手机屏上方
//            String notifyTag = notifyId + "jpush";//由于同一条消息 id 一样 ，有针对悬挂式通知打了一个tag；
//            nm.notify(notifyTag, notifyId, builder.build());
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        Thread.sleep(5000);//五秒后悬挂式通知消失
//                        nm.cancel(notifyTag, notifyId);//按tag id 来清除消息
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
//        }
//    }
}
